define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();

            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.length > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },

        table: {
            subject: function () {
                Table.api.init({
                    extend: {
                        index_url: 'subject/subjectrecycle/index',
                        del_url: 'subject/subjectrecycle/del',
                        reduction_url: 'subject/subjectrecycle/reduction',
                        multi_url: 'subject/subjectrecycle/multi',
                        table: 'subject',
                    },
                });
                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    toolbar: '#toolbar1',
                    sortName: 'createtime',
                    search: false, // 隐藏快速搜索框
                    showToggle: false, // 隐藏切换按扭
                    columns: [
                        [
                            { checkbox: true },
                            { field: 'id', title: __('Id') },
                            { field: 'category.name', title: __('Sname'), operate: 'LIKE' },// 模糊查询
                            { field: 'title', title: __('Otitle'), operate: 'LIKE' },
                            { field: 'thumbs', title: __('Sthumbs'), operate: false, formatter: Table.api.formatter.image },
                            { field: 'price', title: __('Sprice') },
                            { field: 'likes_text', title: __('Slikes'), operate: false },
                            { field: 'createtime', title: __('Ocreatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                            {
                                field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                                buttons: [
                                    {
                                        name: '还原',
                                        title: __('确认是否还原'),
                                        classname: 'btn btn-xs btn-success btn-ajax',
                                        icon: 'fa fa-mail-reply',
                                        confirm: '确认还原',
                                        url: 'subject/subjectrecycle/reduction',
                                        refresh: true, //操作完成后是否刷新列表
                                    },
                                ]
                            }
                        ]
                    ]
                });

                // 还原事件
                $("#toolbar1 .btn-reduction").click(function () {
                    let ids = Table.api.selectedids(table1) //获取选中的条目ID集合
                    layer.confirm('确定要还原吗?', { title: '还原', btn: ['是', '否'] }, function (index) {
                        //发送ajax请求
                        Backend.api.ajax(
                            //请求地址
                            { url: `subject/subjectrecycle/reduction?ids=${ids}` },
                            //回调函数
                            function () {
                                // 关闭窗口
                                Layer.close(index)

                                // 刷新数据表格
                                table1.bootstrapTable('refresh')
                            }
                        )
                    })
                })

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            order: function () {
                Table.api.init({
                    extend: {
                        index_url: 'subject/orderrecycle/index',
                        del_url: 'subject/orderrecycle/del',
                        reduction_url: 'subject/orderrecycle/reduction',
                        multi_url: 'subject/orderrecycle/multi',
                        table: 'subject_order',
                    },
                });
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    toolbar: '#toolbar2',
                    sortName: 'createtime',
                    search: false, // 隐藏快速搜索框
                    showToggle: false,
                    columns: [
                        [
                            { checkbox: true },
                            { field: 'id', title: __('Id') },
                            { field: 'subject.title', title: __('Otitle'), operate: 'LIKE' },
                            { field: 'business.nickname', title: __('Onickname'), operate: 'LIKE' },
                            { field: 'code', title: __('Ocode'), operate: 'LIKE' },// 模糊查询
                            { field: 'total', title: __('Ototal') },
                            { field: 'createtime', title: __('Ocreatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                            {
                                field: 'operate', title: __('Operate'), table: table2, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                                buttons: [
                                    {
                                        name: '还原',
                                        title: __('确认是否还原'),
                                        classname: 'btn btn-xs btn-success btn-ajax',
                                        icon: 'fa fa-mail-reply',
                                        confirm: '确认还原',
                                        url: 'subject/orderrecycle/reduction',
                                        refresh: true, //操作完成后是否刷新列表
                                    },
                                ]
                            }
                        ]
                    ]
                });

                // 还原事件
                $("#toolbar2 .btn-reduction").click(function () {
                    let ids = Table.api.selectedids(table2) //获取选中的条目ID集合
                    layer.confirm('确定要还原吗?', { title: '还原', btn: ['是', '否'] }, function (index) {
                        //发送ajax请求
                        Backend.api.ajax(
                            //请求地址
                            { url: `subject/orderrecycle/reduction?ids=${ids}` },
                            //回调函数
                            function () {
                                // 关闭窗口
                                Layer.close(index)

                                // 刷新数据表格
                                table2.bootstrapTable('refresh')
                            }
                        )
                    })
                })


                // 为表格2绑定事件
                Table.api.bindevent(table2);
            }
        },
        del: function () {
            Controller.api.bindevent();
        },
        reduction: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});
